package com.elective.courseselect.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.elective.courseselect.dao.CharacterDao;
import com.elective.courseselect.dao.CourseCharacterDao;
import com.elective.courseselect.dao.QuestionDao;
import com.elective.courseselect.entity.CharacterEntity;
import com.elective.courseselect.entity.CourseEntity;
import com.elective.courseselect.entity.QuestionEntity;
import com.elective.courseselect.enums.CharacterType;
import com.elective.courseselect.exception.RRException;
import com.elective.courseselect.service.CharacterService;
import com.elective.courseselect.utils.R;
import com.elective.status.StatusCode;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.StringJoiner;


@Service("characterService")
@Transactional(rollbackFor = Exception.class)
public class CharacterServiceImpl extends ServiceImpl<CharacterDao, CharacterEntity> implements CharacterService {

    @Autowired
    CharacterDao characterDao;

    @Autowired
    CourseCharacterDao courseCharacterDao;

    @Autowired
    QuestionDao questionDao;


    @Override
    public R queryPage(Map<String, Object> params) {
        List<CharacterEntity> characterEntities = characterDao.selectList(null);
        for (CharacterEntity record : characterEntities) {
            if (record.getType() == CharacterType.CHARACTER_LABEL) {
                record.setTypeName("性格标签");
            }
            if (record.getType() == CharacterType.CLASS_LABEL) {
                record.setTypeName("课程标签");
            }
        }
        return R.ok(StatusCode.OK, "查询标签成功").put("list", characterEntities);
    }

    @Override
    public R saveNew(CharacterEntity character) {
        QueryWrapper<CharacterEntity> characterEntityQueryWrapper = new QueryWrapper<>();
        characterEntityQueryWrapper.eq("label", character.getLabel());
        characterEntityQueryWrapper.eq("type", character.getType());
        List<CharacterEntity> list = this.list(characterEntityQueryWrapper);
        if (list.size() > 0) {
            return R.error(StatusCode.ERROR, "数据库以存在");
        } else {
            boolean save = this.save(character);
        }
        return R.ok();
    }

    @Override
    public R updateByIdNew(CharacterEntity character) {
        CharacterEntity byId = this.getById(character.getId());
        if (null == byId) {
            return R.error(StatusCode.ERROR, "id不存在");
        }
        boolean b = this.updateById(character);
        return R.ok("修改标签成功");
    }

    @Override
    public void deleteCharacter(List<Integer> ids) {
        for (Integer id : ids) {
            // 查询是否被课程引用
            List<CourseEntity> courseEntities = courseCharacterDao.queryByCharacterId(id);
            // 查询是否被问题引用
            List<QuestionEntity> questionEntities = questionDao.queryByCharacterId(id);

            boolean flag = false;
            StringJoiner errorMsg = new StringJoiner(",");
            if (CollectionUtils.isNotEmpty(courseEntities)) {
                errorMsg.add("该标签被以下课程引用");
                courseEntities.forEach(courseEntity -> errorMsg.add(courseEntity.getName()));
                flag = true;
            }

            if (CollectionUtils.isNotEmpty(questionEntities)) {
                errorMsg.add("该标签被以下问卷题目引用");
                for (QuestionEntity questionEntity : questionEntities) {
                    errorMsg.add(questionEntity.getTitle());
                }
                flag = true;
            }

            if (Boolean.TRUE.equals(flag)) {
                throw new RRException(errorMsg.toString());
            }

            characterDao.deleteById(id);
        }
    }


    /**
     * Application构建查询对象
     *
     * @param characterEntity
     * @return
     */
    public static QueryWrapper createExample(CharacterEntity characterEntity) {
        QueryWrapper<CharacterEntity> wrapper = new QueryWrapper<>();
        if (characterEntity != null) {
            //
            if (!StringUtils.isEmpty(characterEntity.getId())) {
                wrapper.eq("id", characterEntity.getId());
            }
            //
            if (!StringUtils.isEmpty(characterEntity.getLabel())) {
                wrapper.eq("label", characterEntity.getLabel());
            }
            if (!StringUtils.isEmpty(characterEntity.getType())) {
                wrapper.eq("type", characterEntity.getType());
            }
        }
        return wrapper;
    }

}